package cn.rmc.hrm.service.impl;

import cn.rmc.hrm.constants.BaseConstant;
import cn.rmc.hrm.doc.CourseDoc;
import cn.rmc.hrm.domain.Course;
import cn.rmc.hrm.domain.CourseDetail;
import cn.rmc.hrm.domain.CourseMarket;
import cn.rmc.hrm.domain.CourseType;
import cn.rmc.hrm.feign.CourseFeign;
import cn.rmc.hrm.mapper.CourseDetailMapper;
import cn.rmc.hrm.mapper.CourseMapper;
import cn.rmc.hrm.mapper.CourseMarketMapper;
import cn.rmc.hrm.mapper.CourseTypeMapper;
import cn.rmc.hrm.service.ICourseService;
import cn.rmc.hrm.util.AjaxResult;
import cn.rmc.hrm.util.AssertUtils;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import dto.CourseDto;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import to.CourseStationMessageTo;
import to.EmailTo;
import to.SmsTo;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author whale.chen
 * @since 2021-08-19
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {
    @Autowired
    private CourseDetailMapper courseDetailMapper;
    @Autowired
    private CourseTypeMapper courseTypeMapper;
    @Autowired
    private CourseMarketMapper courseMarketMapper;
    @Autowired
    private CourseFeign courseFeign;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    /**
     * 新增课程信息
     * @param courseDto
     */
    @Override
    public void save(CourseDto courseDto) {
        Course course = courseDto.getCourse();
        CourseDetail detail = courseDto.getCourseDetail();
        CourseMarket market = courseDto.getCourseMarket();
//        校验参数
        AssertUtils.isNull(course,"课程输入数据错误");
//        保存课程信息
        course.setStatus(BaseConstant.CourseConstant.STATE_OFFLINE);
        course.setUserId(42L);
        course.setUserName("任大师");
        course.setTenantId(49L);
        course.setTenantName("无敌java");
        super.insert(course);
//        CourseDetail保存course id
        detail.setId(course.getId());
        courseDetailMapper.insert(detail);
//        CourseMarket保存course id
        market.setId(course.getId());
        courseMarketMapper.insert(market);
//        课程类型+1
        CourseType courseType = courseTypeMapper.selectById(course.getCourseTypeId());
        courseType.setTotalCount(courseType.getTotalCount()+1);
        courseTypeMapper.updateById(courseType);
    }

    /**
     * 课程上线
     * @param id
     */
    @Override
    public void onLineCourse(Long id) {
//        查找对应的课程
        Course course = super.selectById(id);
        AssertUtils.isNull(course,"不能为空");
//        课程状态是否为下架状态
        boolean isTrue  = course.getStatus()==BaseConstant.CourseConstant.STATE_OFFLINE;
        AssertUtils.isFalse(isTrue,"课程已下线");
//        修改状态，和上架时间
        course.setStatus(BaseConstant.CourseConstant.STATE_ONLINE);
        course.setOnlineTime(new Date());
        super.updateById(course);
//        根据id查找market
        CourseMarket market = courseMarketMapper.selectById(id);
//        创建文档对象
        CourseDoc courseDoc = new CourseDoc();
//        将数据存到文档对象里
        BeanUtils.copyProperties(course,courseDoc);
        BeanUtils.copyProperties(market,courseDoc);
//        设置价格
        courseDoc.setPrice(market.getPrice().doubleValue());
        courseDoc.setPriceOld(market.getPriceOld().doubleValue());
//        将数据转存到es队列中
        AjaxResult result = courseFeign.save(courseDoc);
        AssertUtils.isFalse(result.isSuccess(),result.getMessage());
//        通过rabbitMQ发布消息
        sendMessage(course);

    }

    private void sendMessage(Course course) {
        //        发送短信
        String smsConstent = "尊敬的用户"+course.getName()+"上线了快来使用吧！！！！";
        List<String> phones = Arrays.asList("15775954185","1598263705","15884963258");
        rabbitTemplate.convertAndSend(
                BaseConstant.CourseConstant.EXCHANGE_TOPICS_COURSE,//交换机
                "course.sms",
                new SmsTo(smsConstent,phones)
        );
//        发送邮件
        String emailConstent = "尊敬的用户"+course.getName()+"上线了快来使用吧！！！！";
        List<String> email = Arrays.asList("15775954185@163.com","1598263705@qq.com","15884963258@outlook.com");
        rabbitTemplate.convertAndSend(
                BaseConstant.CourseConstant.EXCHANGE_TOPICS_COURSE,//交换机
                "course.email",
                new EmailTo(emailConstent,email)
        );
//        发送站内信
        CourseStationMessageTo messageTo = new CourseStationMessageTo();
        messageTo.setTitle(course.getName()+"上线了！");
        messageTo.setContent("尊敬的用户"+course.getName()+"上线了快来使用吧！！！！");
        messageTo.setResources(course.getPic());
        messageTo.setUserIds(Arrays.asList(1L,2L,3L,4L));
        messageTo.setState(0);
        messageTo.setTime(new Date());
        messageTo.setType("系统消息");
        rabbitTemplate.convertAndSend(
                BaseConstant.CourseConstant.EXCHANGE_TOPICS_COURSE, //交换机
                "course.station",
                messageTo
        );
    }
}
